package com.example.service.impl;

import com.example.mapper.DateMessageMapper;
import com.example.model.DateMessage;
import com.example.service.IDateMessageService;
import com.example.utils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.Date;
import java.util.List;

public class DateMessageServiceImpl implements IDateMessageService {

    public int addDateMessage(DateMessage dateMessage) {
        // 1、获取sqlSessionFactory对象
        SqlSessionFactory sqlSessionFactory = MyBatisUtils.getSqlSessionFactory();
        // 2、获取sqlSession对象
        SqlSession openSession = sqlSessionFactory.openSession();
        try {
            // 3、获取接口的实现类对象
            //会为接口自动的创建一个代理对象，代理对象去执行增删改查方法
            DateMessageMapper dateMessageMapper = openSession.getMapper(DateMessageMapper.class);
            int i = dateMessageMapper.add(dateMessage);
            openSession.commit();
            return i;
        } finally {
            openSession.close();
        }
    }

    public List<DateMessage> queryByGroupIdAndQQ(Date start, Date end, Long fromGroup, Long fromQQ) {
        // 1、获取sqlSessionFactory对象
        SqlSessionFactory sqlSessionFactory = MyBatisUtils.getSqlSessionFactory();
        // 2、获取sqlSession对象
        SqlSession openSession = sqlSessionFactory.openSession();
        try {
            // 3、获取接口的实现类对象
            //会为接口自动的创建一个代理对象，代理对象去执行增删改查方法
            DateMessageMapper dateMessageMapper = openSession.getMapper(DateMessageMapper.class);
            return dateMessageMapper.queryByGroupIdAndQQ(start,end,fromGroup,fromQQ);
        } finally {
            openSession.close();
        }
    }

    public List<DateMessage> queryByGroupId(Date start, Date end, Long fromGroup) {
        // 1、获取sqlSessionFactory对象
        SqlSessionFactory sqlSessionFactory = MyBatisUtils.getSqlSessionFactory();
        // 2、获取sqlSession对象
        SqlSession openSession = sqlSessionFactory.openSession();
        try {
            // 3、获取接口的实现类对象
            //会为接口自动的创建一个代理对象，代理对象去执行增删改查方法
            DateMessageMapper dateMessageMapper = openSession.getMapper(DateMessageMapper.class);
            return dateMessageMapper.queryByGroupId(start,end,fromGroup);
        } finally {
            openSession.close();
        }
    }

    public List<DateMessage> queryPeriod(Date start, Date end, String timeStart, String timeEnd, Long fromGroup, Long fromQQ) {
        // 1、获取sqlSessionFactory对象
        SqlSessionFactory sqlSessionFactory = MyBatisUtils.getSqlSessionFactory();
        // 2、获取sqlSession对象
        SqlSession openSession = sqlSessionFactory.openSession();
        try {
            // 3、获取接口的实现类对象
            //会为接口自动的创建一个代理对象，代理对象去执行增删改查方法
            DateMessageMapper dateMessageMapper = openSession.getMapper(DateMessageMapper.class);
            return dateMessageMapper.queryPeriod(start,end,timeStart,timeEnd,fromGroup,fromQQ);
        } finally {
            openSession.close();
        }
    }
}
